﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace BasicComponents.pub
{
    /// <summary>
    /// Interaction logic for UC_LabelTextbox.xaml
    /// </summary>
    public partial class UC_LabelTextbox : UserControl
    {
        public UC_LabelTextbox()
        {
            InitializeComponent();
        }
        TextChangedEventArgs textChangedEventArgs = null;
        public TextChangedEventArgs TextChangedEventArgs
        {
            get { return textChangedEventArgs; }
            set { textChangedEventArgs = value; }
        }

        //该参数保存输入框监测到的输入事件
        KeyEventArgs keyEventArgs = null;
        public KeyEventArgs KeyEventArgs
        {
            get { return keyEventArgs; }
            set { keyEventArgs = value; }
        }

        static void resetComponentSize(UC_LabelTextbox sender)
        {
            // sender.Width = sender.column_title.Width.Value + sender.column_value.Width.Value + sender.columns_btn.Width.Value;
        }
        //1、标题
        public String TitileVal
        {
            get { return (String)GetValue(TitileValProperty); }
            set { SetValue(TitileValProperty, value); }
        }
        public static readonly DependencyProperty TitileValProperty =
            DependencyProperty.Register("TitileVal", typeof(String), typeof(UC_LabelTextbox), new PropertyMetadata("标题", (s, e) =>
            {
                UC_LabelTextbox sender = s as UC_LabelTextbox;
                sender.label_titile.Content = e.NewValue.ToString();
                // resetComponentSize(sender);
            }));
        //只读属性
        public Boolean TxtBoxIsReadyOnly
        {
            get { return (Boolean)GetValue(TxtBoxIsReadyOnlyProperty); }
            set { SetValue(TxtBoxIsReadyOnlyProperty, value); }
        }
        public static readonly DependencyProperty TxtBoxIsReadyOnlyProperty =
            DependencyProperty.Register("TxtBoxIsReadyOnly", typeof(Boolean), typeof(UC_LabelTextbox), new PropertyMetadata(false, (s, e) =>
            {
                UC_LabelTextbox sender = s as UC_LabelTextbox;
                sender.txt_value.IsReadOnly = (Boolean)e.NewValue;
                if ((Boolean)e.NewValue)
                {
                   sender.txt_value.Background = Brushes.WhiteSmoke;
                }
                else
                { 
                    sender.txt_value.Background = null;
                }

                // resetComponentSize(sender);
            }));
          
        //、输入框长度 
        public   Double TxtWidth
        {
            get { return (Double)GetValue(TxtWidthProperty); }
            set { SetValue(TxtWidthProperty, value); }
        }
        public static readonly  DependencyProperty TxtWidthProperty = DependencyProperty.Register("TxtWidth", typeof(Double), typeof(UC_LabelTextbox), new PropertyMetadata(120.00, (s, e) =>
        {
            UC_LabelTextbox sender = s as UC_LabelTextbox;
            sender.txt_value.Width = Double.Parse(e.NewValue.ToString());
        }));

        //、标题长度 
        public Double TitleWidth
        {
            get { return (Double)GetValue(TitleWidthProperty); }
            set { SetValue(TitleWidthProperty, value); }
        }


        public static readonly DependencyProperty TitleWidthProperty = DependencyProperty.Register("TitleWidth", typeof(Double), typeof(UC_LabelTextbox), new PropertyMetadata(120.00, (s, e) =>
        {
            UC_LabelTextbox sender = s as UC_LabelTextbox;
            sender.label_titile.Width = Double.Parse(e.NewValue.ToString());
        }));
        //、整体框高度
        public new Double Height
        {
            get { return (Double)GetValue(HeightProperty); }
            set { SetValue(HeightProperty, value); }
        }


        public static readonly new DependencyProperty HeightProperty = DependencyProperty.Register("Height", typeof(Double), typeof(UC_LabelTextbox), new PropertyMetadata(120.00, (s, e) =>
         {
             UC_LabelTextbox sender = s as UC_LabelTextbox;
             sender.txt_value.Height = Double.Parse(e.NewValue.ToString());
             sender.label_titile.Height = Double.Parse(e.NewValue.ToString());
         }));

        //enable属性
        public Boolean TxtBoxIsEnabled
        {
            get { return (Boolean)GetValue(TxtBoxIsEnabledProperty); }
            set { SetValue(TxtBoxIsEnabledProperty, value); }
        }
        public static readonly DependencyProperty TxtBoxIsEnabledProperty =
            DependencyProperty.Register("TxtBoxIsEnabled", typeof(Boolean), typeof(UC_LabelTextbox), new PropertyMetadata(true, (s, e) =>
            {
                UC_LabelTextbox sender = s as UC_LabelTextbox;
                sender.txt_value.IsEnabled = (Boolean)e.NewValue;
                if ((Boolean)e.NewValue)
                {
                    sender.txt_value.Background = Brushes.LightGray;
                }
                else
                {
                    sender.txt_value.Background = null;
                }

                // resetComponentSize(sender);
            }));

        //2、内容块内容信息
        public String TxtValue
        {
            get { return (String)GetValue(TxtValueProperty); }
            set { SetValue(TxtValueProperty, value); }
        }
        public static readonly DependencyProperty TxtValueProperty = DependencyProperty.Register("TxtValue", typeof(String), typeof(UC_LabelTextbox), new PropertyMetadata("", (s, e) =>
        {
            UC_LabelTextbox sender = s as UC_LabelTextbox;
            sender.txt_value.Text = e.NewValue.ToString();
            //resetComponentSize(sender);
        })); 
     
        //5、内容输入事件
        public event RoutedEventHandler TxtKeyDown
        {
            add { this.AddHandler(TxtKeyDownEvent, value); }
            remove { this.RemoveHandler(TxtKeyDownEvent, value); }
        }
        public static readonly RoutedEvent TxtKeyDownEvent = EventManager.RegisterRoutedEvent("TxtKeyDown", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(UC_LabelTextbox));

        //输入框内容变化事件
        private void txt_value_TextChanged(object sender, TextChangedEventArgs e)
        {
            try
            {
                this.TxtValue = this.txt_value.Text;
                TextChangedEventArgs args = new TextChangedEventArgs(UC_LabelTextbox.TxtValueChangedEvent, e.UndoAction, e.Changes);
                RaiseEvent(args);
            }
            catch (Exception e1)
            {
                File.AppendAllText("log_wxt.txt", "----" + DateTime.Now.ToString("yyyyMMdd:hh mm ss") + "----\r\n" + "\r\n" + e1.Message);
            }
        }

        //输入框内容变化事件
        public event RoutedEventHandler TxtValueChanged
        {
            add { this.AddHandler(TxtValueChangedEvent, value); }
            remove { this.RemoveHandler(TxtValueChangedEvent, value); }
        }

        // Using a DependencyProperty as the backing store for TxtValueChanged.  This enables animation, styling, binding, etc...
        public static readonly RoutedEvent TxtValueChangedEvent =
            EventManager.RegisterRoutedEvent("TxtValueChanged", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(UC_LabelTextbox));


        //按键按下事件
        private void txt_value_KeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                this.keyEventArgs = e;
                RoutedEventArgs args = new RoutedEventArgs(UC_LabelTextbox.TxtKeyDownEvent, this); 
                RaiseEvent(args);
            }
            catch (Exception e1)
            {
                MessageBox.Show("" + e1.Message);
            }
        }
    }
}
